

//
//  YYYWelcomeView.swift
//  HotNews
//
//  Created by YYY on 2017/10/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

// MARK: 欢迎界面
class YYYWelcomeView: UIView {

    //背景图片
    private lazy var backgroundImageView = UIImageView(image: UIImage(named: "launch"))
    //头像
    private lazy var portraitImageView = UIImageView(frame: CGRect(x: 0, y: kDeviceHeight-250, width: 85, height: 85))
    //昵称
    private lazy var nickName = UILabel(text: YYYNetworkManager.shared.userAccount.screen_name ?? "欢迎归来", fontSize: 17, color: UIColor.darkGray)
    
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        //初始化控件
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToWindow() {
        super.didMoveToWindow()
        //添加动画
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations:
            {
            self.portraitImageView.setY(newY: 160)
            self.nickName.setY(newY: self.portraitImageView.bottom + 15)
        }) { (_) in
            UIView.animate(withDuration: 1, delay: 0, options: [], animations: {
                
                self.nickName.alpha = 1
            }, completion: { (_) in
                //移除视图
                self.removeFromSuperview()
            })
        }
    }
}

extension YYYWelcomeView
{
    //初始化控件
    private func setupUI()
    {
        addSubview(backgroundImageView)
        addSubview(portraitImageView)
        addSubview(nickName)
        //背景图片
        backgroundImageView.frame = kMainScreenBounds

        //设置头像URL
        let url = URL(string: YYYNetworkManager.shared.userAccount.avatar_large ?? "")
        portraitImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        portraitImageView.center.x = center.x
        //设置圆角
        portraitImageView.layer.masksToBounds = true
        portraitImageView.layer.cornerRadius = portraitImageView.width * 0.5
        
        //昵称
        nickName.frame = CGRect(x: 0, y: portraitImageView.bottom+15, width: kDeviceWidth, height: 30)
        nickName.alpha = 0
        nickName.textAlignment = .center
        nickName.center.x = portraitImageView.center.x
        
        
    }
}
